/**
 * @author zhao
 * Projet : Calculatrice
 */

package fr.zands.calculatrice;

import org.wltea.expression.ExpressionEvaluator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class calculatrice extends Activity {

	private EditText Res_text; // le champ pour afficher le r�sultats ou la
	// formule
	private Button[] btchiffres = new Button[10]; // les buttons de ciffres
	// :0,1,2,3,4,5,6,7,8,9
	private Button[] btoprateurs = new Button[7]; // les buttons des op�rateurs:
	// +, -, *, /, %, x�,
	// racine, del, maz

	private Button[] btautres = new Button[4]; // ., (, )
	// private OnClickListener actionPerformed;

	private String command;
	private String str; // le text actu.
	private boolean bool_c = false;
	private double r1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Res_text = (EditText) findViewById(R.id.editText1);
		Res_text.setText("");

		btchiffres[0] = (Button) findViewById(R.id.but_0);
		btchiffres[1] = (Button) findViewById(R.id.but_1);
		btchiffres[2] = (Button) findViewById(R.id.but_2);
		btchiffres[3] = (Button) findViewById(R.id.but_3);
		btchiffres[4] = (Button) findViewById(R.id.but_4);
		btchiffres[5] = (Button) findViewById(R.id.but_5);
		btchiffres[6] = (Button) findViewById(R.id.but_6);
		btchiffres[7] = (Button) findViewById(R.id.but_7);
		btchiffres[8] = (Button) findViewById(R.id.but_8);
		btchiffres[9] = (Button) findViewById(R.id.but_9);

		btoprateurs[0] = (Button) findViewById(R.id.but_add); // addition +
		btoprateurs[1] = (Button) findViewById(R.id.but_moins); // moins -
		btoprateurs[2] = (Button) findViewById(R.id.but_multi); // multiple *
		btoprateurs[3] = (Button) findViewById(R.id.but_div); // division /
		btoprateurs[4] = (Button) findViewById(R.id.but_mod); // mod %
		btoprateurs[5] = (Button) findViewById(R.id.but_supp); // delete
		btoprateurs[6] = (Button) findViewById(R.id.but_maz); // metrre � zero

		btautres[0] = (Button) findViewById(R.id.but_point); // point .
		btautres[1] = (Button) findViewById(R.id.but_eq); // car�e x�
		btautres[2] = (Button) findViewById(R.id.but_carre); // car�e x�
		btautres[3] = (Button) findViewById(R.id.but_rac); // racine

		for (int i = 0; i < 10; ++i) {

			btchiffres[i].setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					command = ((Button) arg0).getText().toString();
					if (bool_c == false) {
						Res_text.append(command);
					} else {
						Res_text.setText(command);
						bool_c = false;
					}

				}
			});

			if (i < 5) {
				btoprateurs[i].setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						bool_c = false;
						command = ((Button) v).getText().toString();
						str = Res_text.getText().toString();
						if (str.length() > 0) {
							Res_text.append(command);
						}
					}
				});
			}
		}

		btoprateurs[5].setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				str = Res_text.getText().toString();
				if (str.length() > 1) {
					str = str.substring(0, str.length() - 1);
					Res_text.setText(str);
				} else {
					Res_text.setText("");
				}
			}
		});

		btoprateurs[6].setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Res_text.setText("");
			}
		});

		btautres[0].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				command = ((Button) v).getText().toString();
				Res_text.append(command);

			}

		});

		for (int i = 1; i < 4; ++i) {
			btautres[i].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					bool_c = true;
					str = Res_text.getText().toString();

					if (str.length() > 0) {
						command = ((Button) v).getText().toString();
						Object result = ExpressionEvaluator.evaluate(str, null);
						if (command.compareTo("=") == 0) { // Res_text.setText(Double.toString(((double)
							// result));
							 r1 = Double.parseDouble(result.toString());
							Res_text.setText(Double.toString(r1));
						} else if (command.compareTo("x²") == 0) {
							 r1 = Double.parseDouble(result.toString());
							Res_text.setText(Double.toString(r1 * r1));
						} else if (command.compareTo("Racine") == 0) {
							 r1 = Double.parseDouble(result.toString());
							r1 = Math.sqrt(r1);
							Res_text.setText(Double.toString(r1));
						}
						/*
						 * Toast.makeText(calculatrice.this, result.toString(),
						 * Toast.LENGTH_SHORT) .show();
						 */
					}
				}
			});
		}
	}
}